/*
 * Copyright 2010 Chad Retz
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.jdocng.shared.model;

import javax.xml.bind.annotation.XmlType;

import com.google.common.annotations.GwtCompatible;
import com.google.gson.annotations.Expose;
import com.sun.javadoc.Type;

/**
 * Implementation of {@link Type}
 *
 * @author Chad Retz
 */
@XmlType(name = "type")
@GwtCompatible(serializable = true)
@SuppressWarnings("serial")
public class TypeImpl extends JavadocNgEntity implements Type {
    
    //exposed fields
    
    @Expose
    private String dimension;
    
    @Expose
    private boolean primitive;
    
    @Expose
    private String qualifiedTypeName;
    
    @Expose
    private String simpleTypeName;
    
    @Expose
    private String typeName;
    
    //non-exposed fields
    
    private transient ClassDocImpl classLoaded;
    
    //Type interface impl
    
    @Override
    public AnnotationTypeDocImpl asAnnotationTypeDoc() {
        if (asClassDoc() instanceof AnnotationTypeDocImpl) {
            return (AnnotationTypeDocImpl) asClassDoc();
        }
        return null;
    }
    
    @Override
    public ClassDocImpl asClassDoc() {
        if (classLoaded == null) {
            classLoaded = getContext().getClassDoc(qualifiedTypeName);
        }
        return classLoaded;
    }
    
    @Override
    public ParameterizedTypeImpl asParameterizedType() {
        if (this instanceof ParameterizedTypeImpl) {
            return (ParameterizedTypeImpl) this;
        }
        return null;
    }
    
    @Override
    public TypeVariableImpl asTypeVariable() {
        if (this instanceof TypeVariableImpl) {
            return (TypeVariableImpl) this;
        }
        return null;
    }
    
    @Override
    public WildcardTypeImpl asWildcardType() {
        if (this instanceof WildcardTypeImpl) {
            return (WildcardTypeImpl) this;
        }
        return null;
    }
    
    @Override
    public String dimension() {
        return dimension;
    }
    
    @Override
    public boolean isPrimitive() {
        return primitive;
    }
    
    @Override
    public String qualifiedTypeName() {
        return qualifiedTypeName;
    }
    
    @Override
    public String simpleTypeName() {
        return simpleTypeName;
    }
    
    @Override
    public String toString() {
        throw new UnsupportedOperationException("Not sure yet...");
    }
    
    @Override
    public String typeName() {
        return typeName;
    }
    
    //other accessors
    
    public void setDimension(String dimension) {
        this.dimension = dimension;
    }
    
    public void setPrimitive(boolean primitive) {
        this.primitive = primitive;
    }
    
    public void setQualifiedTypeName(String qualifiedTypeName) {
        this.qualifiedTypeName = qualifiedTypeName;
    }
    
    public void setSimpleTypeName(String simpleTypeName) {
        this.simpleTypeName = simpleTypeName;
    }
    
    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }
}
